package com.greenlife.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.greenlife.commom.PageInfo;
import com.greenlife.commom.ResponseException;
import com.greenlife.common.enums.BaseResultCodeEnum;
import com.greenlife.common.enums.RoleIDEnum;
import com.greenlife.converter.PoConverterModel;
import com.greenlife.dao.UserMapper;
import com.greenlife.model.User;
import com.greenlife.model.po.UserPo;
import com.greenlife.service.UserService;

@Service("userService")
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper userMapper;

	@Override
	public int deleteByPrimaryKey(User user) {
		int value = userMapper.deleteByPrimaryKey(user);
		if (value == 1) {
			return 1;
		} else {
			throw new ResponseException(BaseResultCodeEnum.ERR_CODE_40000);
		}
	}

	@Override
	public User insertSelective(User user) {
		try {
			/** 初始化用户数据 **/
			user.setSex("男");
			user.setRoleId(RoleIDEnum.NORMAL_USER);

			int value = userMapper.insertSelective(user);
			if (value == 1)
				return user;
			else {
				throw new ResponseException(BaseResultCodeEnum.ERR_CODE_30009);
			}
		} catch (Exception e) {
			throw new ResponseException(BaseResultCodeEnum.ERR_CODE_10000);
		}

	}

	@Override
	public User selectByPrimaryKey(User user) {
		User value = userMapper.selectByPrimaryKey(user);
		if (value == null) {
			throw new ResponseException(BaseResultCodeEnum.ERR_CODE_40000);
		}
		return value;
	}

	@Override
	public int updateByPrimaryKeySelective(User user) {
		try {
			return userMapper.updateByPrimaryKeySelective(user);
		} catch (Exception e) {
			throw new ResponseException(BaseResultCodeEnum.ERR_CODE_10001);
		}
	}

	@Override
	public User selectByUser(User user) {
		try {
			User result = userMapper.selectByUser(user);
			if (result != null)
				return result;
			else {
				throw new ResponseException(BaseResultCodeEnum.ERR_CODE_30008);
			}
		} catch (Exception e) {
			throw new ResponseException(BaseResultCodeEnum.ERR_CODE_10003);
		}

	}

	@Override
	public List<User> selectUserByPage(User user, PageInfo pageInfo) {
		try {
			List<User> users = userMapper.selectUserByPage(user, pageInfo);
			int items = userMapper.selectUserCount(user);
			pageInfo.setItems(items);
			pageInfo.setTotalPage((items - 1) / pageInfo.getItemsPerPage() + 1);
			pageInfo.setCurItems(users.size());
			return users;
		} catch (Exception e) {
			throw new ResponseException(BaseResultCodeEnum.ERR_CODE_10003);
		}
	}

	@Override
	public int updateUserPassword(UserPo userPo) {
		User user = PoConverterModel.userPoToUser(userPo);
		User value = userMapper.selectByPrimaryKey(user);
		if (value == null || !(value.getPassword().equals(userPo.getOldPassword()))) {
			throw new ResponseException(BaseResultCodeEnum.ERR_CODE_20007);
		}
		if (user.getId() != null && user.getPassword() != null && !(user.getPassword().equals(""))) {
			return userMapper.updateByPrimaryKeySelective(user);
		} else {
			throw new ResponseException(BaseResultCodeEnum.ERR_CODE_10001);
		}

	}

}
